﻿using System;
using System.Collections;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SiteFox.WebParts.Standard
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    public class HtmlEditorWP : WebPart
    {
        string _HtmlContent;
        Literal lit_HtmlContent = new Literal();

        public override EditorPartCollection CreateEditorParts()
        {
            ArrayList _editorArray = new ArrayList();
            HtmlEditor_Editor _edPart = new HtmlEditor_Editor();
            _edPart.ID = this.ID + "_editorPart1";
            _editorArray.Add(_edPart);
            EditorPartCollection _editorParts = new EditorPartCollection(_editorArray);
            return _editorParts;
        }

        [Personalizable(true)]
        public string HtmlContent
        {
            get { return _HtmlContent; }
            set { _HtmlContent = value; }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            lit_HtmlContent = new Literal();
            lit_HtmlContent.Text = HtmlContent;
            Controls.Add(lit_HtmlContent);
        }

        // Create a custom EditorPart to edit the WebPart control.
        [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
        private class HtmlEditor_Editor : EditorPart
        {
            Literal _lit_Button = new Literal();
            Literal _lit_PreScript = new Literal();
            Literal _lit_Content = new Literal();
            HiddenField _hf_HtmlValue = new HiddenField();
            Literal _lit_Script = new Literal();
            
            public override bool ApplyChanges()
            {
                HtmlEditorWP _part = (HtmlEditorWP)WebPartToEdit;
                _part.HtmlContent = Uri.UnescapeDataString(_hf_HtmlValue.Value);
                return true;
            }
            public override void SyncChanges()
            {
                EnsureChildControls();
                HtmlEditorWP _part = (HtmlEditorWP)WebPartToEdit;
                if (_part.HtmlContent != null) 
                { 
                    _hf_HtmlValue.Value = Uri.EscapeDataString(_part.HtmlContent);
                }
            }
            protected override void CreateChildControls()
            {
                Controls.Clear();
                HtmlEditorWP _part = (HtmlEditorWP)WebPartToEdit;
                string _AppPath = HttpContext.Current.Request.ApplicationPath;
                if (_AppPath == "/")
                {
                    _AppPath = "";
                }
                _lit_Button = new Literal();
                _lit_Button.Text = "<input id=\"" + _part.ID + "_Button\" type=\"button\" onclick=\"LoadHtmlValue();\" value=\"Rich Text Editor\" />";
                Controls.Add(_lit_Button);
                _lit_PreScript = new Literal();
                _lit_PreScript.Text =   "<script src=\"" + _AppPath + "/System/Controls/3rdPart/tiny_mce/tiny_mce.js\" type=\"text/javascript\"></script>" +
                                        "<script type=\"text/javascript\">" +
                                            "tinyMCE.init({" +
                                                // General options
                                                "mode: \"textareas\"," +
                                                "theme: \"advanced\"," +
                                                "plugins: \"style,layer,table,advhr,advimage,advlink,emotions,inlinepopups,media,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,advlist\"," +

                                                // Theme options
                                                "theme_advanced_buttons1: \"bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect\"," +
                                                "theme_advanced_buttons2: \"cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,forecolor,backcolor\"," +
                                                "theme_advanced_buttons3: \"tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,media,advhr,ltr,rtl,|,fullscreen,|,styleprops,|,cite,abbr,acronym,del,ins,attribs\"," +
                                                "theme_advanced_toolbar_location: \"top\"," +
                                                "theme_advanced_toolbar_align: \"left\"," +
                                                "theme_advanced_statusbar_location: \"bottom\"," +
                                                "theme_advanced_resizing: false," +

                                                // Example content CSS (should be your site CSS)
                                                "content_css: \"css/content.css\"," +

                                                // Drop lists for link/image/media/template dialogs
                                                "template_external_list_url: \"lists/template_list.js\"," +
                                                "external_link_list_url: \"lists/link_list.js\"," +
                                                "external_image_list_url: \"lists/image_list.js\"," +
                                                "media_external_list_url: \"lists/media_list.js\"," +

                                                // Style formats
                                                "style_formats: [" +
			                                        "{ title: 'Bold text', inline: 'b' }," +
			                                        "{ title: 'Red text', inline: 'span', styles: { color: '#ff0000'} }," +
			                                        "{ title: 'Red header', block: 'h1', styles: { color: '#ff0000'} }," +
			                                        "{ title: 'Example 1', inline: 'span', classes: 'example1' }," +
			                                        "{ title: 'Example 2', inline: 'span', classes: 'example2' }," +
			                                        "{ title: 'Table styles' }," +
			                                        "{ title: 'Table row 1', selector: 'tr', classes: 'tablerow1' }" +
		                                        "]," +

                                                // Replace values for the template plugin
                                                "template_replace_values: {" +
                                                    "username: \"Some User\"," +
                                                    "staffid: \"991234\"" +
                                                "}" +
                                            "});" +
                                        "</script>";
                Controls.Add(_lit_PreScript);
                _lit_Content = new Literal();
                _lit_Content.Text = "<div id=\"" + _part.ID + "_Content\" style=\"display: none;\">" +
                                        "<div style=\"background: Black;filter: alpha(opacity=50);opacity: 0.50;height: 100%;width: 100%;z-index: 3;position: absolute;top: 0px;left: 0px;\"></div>" +
                                        "<div style=\"background: #fff;border: solid 3px Black;padding: 3px;width: 1000px;height: 600px; z-index: 4;overflow-y: auto;position: absolute;top: 50%;left: 50%;margin-left: -500px;margin-top: -300px;display: block\">" +
                                            "<div style=\"display: block;float: left;border-bottom: solid 1px #444;width: 100%;font-weight: bold;font-size: 16px;\">" +
                                                "<span style=\"display: block;float: left;width: 70%;\">HTML Editor</span>" +
                                            "</div>" +
                                            "<div style=\"margin-top: 5px;margin-bottom: 5px;width: 100%;height: auto;display: block;float: left;\">" +
                                                "<textarea id='elm1' style=\"height: 500px; width: 100%;\"></textarea>" +
                                                "<input type='button' onclick=\"SaveHtmlValue();\" style=\"float: left; margin: 10px;\" value='Save' />" +
                                                "<input type='button' onclick=\"HideHtmlEditor();\" style=\"float: left; margin: 10px;\" value='Cancel' />" +
                                            "</div>" +
                                        "</div>" +
                                    "</div>";
                Controls.Add(_lit_Content);
                _hf_HtmlValue = new HiddenField();
                _hf_HtmlValue.ID = _part.ID + "_hf_HtmlValue";
                _hf_HtmlValue.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                Controls.Add(_hf_HtmlValue);
                _lit_Script = new Literal();
                _lit_Script.Text = "<script type=\"text/javascript\" language=\"javascript\">" +
                                        "function ShowHtmlEditor() {" +
                                            "document.getElementById('" + _part.ID + "_Content').style.display = 'block';" +
                                            "document.getElementById('" + _part.ID + "_Button').disabled = true;" +
                                        "}" +
                                        "function HideHtmlEditor() {" +
                                            "document.getElementById('" + _part.ID + "_Content').style.display = 'none';" +
                                            "document.getElementById('" + _part.ID + "_Button').disabled = false;" +
                                        "}" +
                                        "function SaveHtmlValue() { " +
                                            "var _hf_HtmlValue = document.getElementById('" + _part.ID + "_hf_HtmlValue');" +
                                            "var _Content = document.getElementById('" + _part.ID + "_Content');" +
                                            "var _Window = document.frames('elm1_ifr');" +
                                            "var _WindowBody = _Window.document.body;" +
                                            "_hf_HtmlValue.value = escape(_WindowBody.innerHTML);" +
                                            "HideHtmlEditor();" +
                                        "}" +
                                        "function LoadHtmlValue() { " +
                                            "var _hf_HtmlValue = document.getElementById('" + _part.ID + "_hf_HtmlValue');" +
                                            "var _Content = document.getElementById('" + _part.ID + "_Content');" +
                                            "var _Window = document.frames('elm1_ifr');" +
                                            "var _WindowBody = _Window.document.body;" +
                                            "_WindowBody.innerHTML = unescape(_hf_HtmlValue.value);" +
                                            "ShowHtmlEditor();" +
                                        "}" +
                                    "</script>";
                Controls.Add(_lit_Script);
            }
        }
    }
}
